package com.share.lottery.activity.fans;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.SystemClock;
import android.provider.MediaStore;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.share.lottery.R;
import com.share.lottery.activity.fab.FabiaoActivtiy;
import com.share.lottery.base.BaseActivity;
import com.share.lottery.utils.CommonUtils;
import com.share.lottery.utils.ImageHelper;



/**
 * @author py
 *选择照片或拍照
 */
public class SelectPhotoActiviy extends BaseActivity {
	private File sdcardTempFile;
	//	private File tempFile = new File(Environment.getExternalStorageDirectory(),
	//			getPhotoFileName());
	private TextView tvXc;
	private TextView tvCamera;

	private int crop = 300;
	private static final int PHOTO_REQUEST_CUT = 3;// 结果

	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.select_pic);
		sdcardTempFile = new File(Environment.getExternalStorageDirectory(), "tmp_pic_" + SystemClock.currentThreadTimeMillis() + ".jpg");
		init();
	}
	public void findView() {
		tvXc = (TextView) findViewById(R.id.tv_sure);
		tvCamera = (TextView) findViewById(R.id.tv_cancle);

	}

	@Override
	public void getData() {
		// TODO Auto-generated method stub

	}

	private TextView titleTv;
	public void initTitle() {
		ImageButton titleLeftImg = (ImageButton) findViewById(R.id.title_left_img);
		TextView titleRightImg = (TextView) findViewById(R.id.title_right_tv);
		titleTv = (TextView) findViewById(R.id.title_tv_center);

		titleLeftImg.setImageResource(R.drawable.back);
		titleTv.setText("发表");
		titleRightImg.setVisibility(View.GONE);
		titleLeftImg.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {
				finish();

			}
		});
	}

	@Override
	public void setData() {


	}

	@Override
	public void setonCliker() {
		tvXc.setOnClickListener(this);
		tvCamera.setOnClickListener(this);

	}
	// 使用系统当前日期加以调整作为照片的名称
	private String getPhotoFileName() {
		Date date = new Date(System.currentTimeMillis());
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"'IMG'_yyyyMMdd_HHmmss");
		return dateFormat.format(date) + ".jpg";
	}
	private File cameraFile;
	public void onClick(View v) {
		File dir = new File(localTempImgDir);
		if (!dir.exists()) {
			dir.mkdir();
		}
		switch (v.getId()) {

		case R.id.tv_sure://相册
			Intent intent = new Intent("android.intent.action.PICK");
			intent.setDataAndType(MediaStore.Images.Media.INTERNAL_CONTENT_URI, "image/*");
			intent.putExtra("output", Uri.fromFile(sdcardTempFile));
			intent.putExtra("crop", "true");
			intent.putExtra("aspectX", 1);// 裁剪框比例
			intent.putExtra("aspectY", 1);
			intent.putExtra("outputX", crop);// 输出图片大小
			intent.putExtra("outputY", crop);
			startActivityForResult(intent, 100);
			break;
		case R.id.tv_cancle://拍摄
			if (!CommonUtils.isExitsSdcard()) {
				Toast.makeText(getApplicationContext(), "SD卡不存在，不能拍照", 0).show();
				return;
			}

			cameraFile = new File(dir.getAbsolutePath(), 
					DateFormat.format("yyyyMMdd_hhmmss",
							Calendar.getInstance(Locale.CHINA)) + ".jpg");
			cameraFile.getParentFile().mkdirs();
			startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE).putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(cameraFile)),
					101);
			break;
		default:
			break;
		}
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	
		if (requestCode == 100) {
			if (data == null ) {
				return;
			}
			Bitmap bmp = BitmapFactory.decodeFile(sdcardTempFile.getAbsolutePath());
			//imageView.setImageBitmap(bmp);
			next(bmp);
		}
		else if(requestCode == 101){
			//			选择拍照

			if (cameraFile != null && cameraFile.exists()){

				String	testPath= cameraFile.getAbsolutePath();
				Log.i("test", "拍照图片路径=="+testPath);
				startPhotoZoom(Uri.fromFile(cameraFile));
			}else {
				Toast.makeText(getApplicationContext(), "获取图片失败，建议从相册选择", 1).show();
			}

		}else if(requestCode==102){
			if (data != null)

				sentPicToNext(data);

		}
	}

	private void startPhotoZoom(Uri uri) {
		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setDataAndType(uri, "image/*");
		// crop为true是设置在开启的intent中设置显示的view可以剪裁
		intent.putExtra("crop", "true");

		// aspectX aspectY 是宽高的比例
		intent.putExtra("aspectX", 1);
		intent.putExtra("aspectY", 1);

		// outputX,outputY 是剪裁图片的宽高
		intent.putExtra("outputX", 480);
		intent.putExtra("outputY", 480);
		intent.putExtra("return-data", true);
		intent.putExtra("noFaceDetection", true);
		startActivityForResult(intent, 102);


	}
	private String localTempImgDir = Environment
			.getExternalStorageDirectory() + "/mytup";
	// 将进行剪裁后的图片传递到下一个界面上
	private void sentPicToNext(Intent picdata) {
		Bundle bundle = picdata.getExtras();
		if (bundle != null) {
			Bitmap photo = bundle.getParcelable("data");
			next(photo);
		}
	}

	private void next(Bitmap photo){

		Bitmap bitmap = ImageHelper.comp(photo);

		File newfile = new File(localTempImgDir + "/"
				+ System.currentTimeMillis() + ".jpg");

		boolean sucess = ImageHelper.saveCompressBitmap(bitmap,
				newfile);


		//	File newFile = new File(testPath);
		if (sucess) {
			String name = newfile.getName();
			String ss = newfile.getAbsolutePath();
			String path =ss.substring(0,ss.lastIndexOf("/"))+"/";
			Log.i("test", "选择的图片路径"+ss);

			Intent intent =  new Intent();
			intent.setClass(getApplicationContext(), FabiaoActivtiy.class);
			intent.putExtra("path", ss);
			startActivity(intent);
			//uploadVoice(path, name);
		}else {
			Log.i("test", "保存图片失败");
		}
	}

}
